package web.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
@Slf4j
public class CrossConfig implements WebMvcConfigurer {

    @Value("${web.cross}")
    private Integer cross;

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        if (this.cross != 1) return;
        log.info("Cross enabled");
        registry.addMapping("/**")
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
                .exposedHeaders("X-TOKEN", "X-TOKEN-EXPIRES")
                //allow headers
                .allowedHeaders("*")
                //allow cookie
                .allowCredentials(true)
                .allowedOriginPatterns("*")
                .maxAge(3600);
    }
}
